//
//  GYConfirmOrderAddressViewController.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/27.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYConfirmOrderAddressViewController.h"
#import "GYNetWorkTools.h"
#import "GYAddNewAddressViewController.h"
#import "GYConfirmOrderAddressTableViewCell.h"
//#import "GYConfirmOrderViewController.h"


@interface GYConfirmOrderAddressViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 数据源 */
@property (nonatomic, strong) NSArray *addressList;
@end

@implementation GYConfirmOrderAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //加载地址
    [self loadUserReceiveAddress];
}

- (void)setupUI {
    self.navigationItem.title = @"收货地址管理";
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CGFLOAT_MIN)];
    
    //设置tableView的尾视图
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    footView.backgroundColor = [UIColor clearColor];
    UIButton *btu = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, ScreenWidth-40, 44)];
    btu.titleLabel.font = [UIFont systemFontOfSize:18];
    [btu setTitle:@"添加新地址" forState:UIControlStateNormal];
    [btu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btu.layer.cornerRadius = 4;
    [btu setBackgroundColor:RGBColor(0, 158, 214)];//RGBColor(71, 163, 39)
    [btu addTarget:self action:@selector(addressBtuClick) forControlEvents:UIControlEventTouchUpInside];
    
    [footView addSubview:btu];
    self.tableView.tableFooterView = footView;
}


- (void)loadUserReceiveAddress {
    [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
        EasyLodingConfig *config = [EasyLodingConfig shared];
        config.showOnWindow = YES;
        
        return config;
    }];

    [GYNetWorkTools requestUserAddress:^(NSDictionary *obj) {
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            NSArray *address_list = [datas objectForKey:@"address_list"];
            self.addressList = [GYRecriveAddress loadUserRecriveAddress:address_list];
            //要比对地址(已经有地址的情况)
            for (GYRecriveAddress *model in self.addressList) {
//                if (self.addressList.count == 1) {
//                    model.selectedState = YES;
//                    self.returnSelectedReceiveAddressCallBack(model);
//                    [self.navigationController popViewControllerAnimated:YES];
                    //直接返回到确认订单信息界面
//                    for (UIViewController *viewController in self.navigationController.viewControllers) {
//                        if ([viewController isMemberOfClass:[GYConfirmOrderViewController class]]) {
//                            [self.navigationController popToViewController:viewController animated:YES];
//                        }
//                    }
//                }else {
                    if ([self.defaultSelectedAddress.address_id isEqualToString:model.address_id]) {
                        model.selectedState = YES;
                    }else {
                        model.selectedState = NO;
                    }
//                }
            }
            [self.tableView reloadData];
            
        }else if (code == 400) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"加载数据出错,请重新加载!"];
        }
    } error:^(NSError *error) {
        [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
        [EasyTextView showText:@"加载数据出错,请重新加载!"];
        
        NSLog(@"loadUserAddress====%@",error);
    }];
}

- (void)addressBtuClick {
    GYAddNewAddressViewController *newAddressVC = [[GYAddNewAddressViewController alloc] initWithNibName:@"GYAddNewAddressViewController" bundle:nil];
    newAddressVC.editState = NO;
    //newAddressVC.orderAddress = YES;
    [self.navigationController pushViewController:newAddressVC animated:YES];
}


#pragma mark -- UITableViewDelegate,UITableViewDataSourc

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.addressList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYConfirmOrderAddressTableViewCell *cell = [GYConfirmOrderAddressTableViewCell cellWithTableView:tableView];
    cell.addressModel = self.addressList[indexPath.section];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        GYRecriveAddress *receiveAddress = self.addressList[indexPath.section];
        for (GYRecriveAddress *model in self.addressList) {
            if ([model.address_id isEqualToString:receiveAddress.address_id]) {
                model.selectedState = YES;
            }else {
                model.selectedState = NO;
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [tableView reloadData];
            self.returnSelectedReceiveAddressCallBack(receiveAddress);
            [self.navigationController popViewControllerAnimated:YES];
        });
    });
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYConfirmOrderAddressTableViewCell *cell = [GYConfirmOrderAddressTableViewCell cellWithTableView:tableView];
    
    return [cell getHeight:self.addressList[indexPath.section]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return  10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    bgView.backgroundColor = [UIColor clearColor];
    
    return bgView;
}

@end
